<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui" xmlns:f="http://java.sun.com/jsf/core"
                xmlns:constants="http://www.tuanzi.com/constants/tags"
                xmlns:security="http://www.springframework.org/security/tags"
                template="/templates/template.xhtml">
    <ui:define name="title">消耗记录</ui:define>
    <ui:define name="content">


        <h:form id="incomeForm">
            <p:dialog header="操作执行中,请稍候" widgetVar="doing" modal="true" closable="false" resizable="false"
                      draggable="false"/>
            <p:defaultCommand target="search"/>
            <div class="topbar clearfix" style="background: -webkit-linear-gradient(top,#f6f7f9 0,#ebedf0 100%);">
                <p:toolbar>
                    <f:facet name="left">
                        <p:commandButton id="toggler" type="button" value="展示选择"
                                         style="width: auto;"
                                         icon="ui-icon-calculator"/>
                        <p:columnToggler datasource="statisticRecordDataTable" trigger="toggler"/>
                    </f:facet>
                    <f:facet name="right">
                        <p:commandButton id="search" value="查询" icon="fa fa-search"
                                         actionListener="#{businessIncomeListBean.count()}"
                                         onstart="PF('doing').show()"
                                         update="@form"/>
                        <p:commandButton value="重置" actionListener="#{businessIncomeListBean.onReset()}"
                                         icon="fa fa-refresh"
                                         process="@this" update="@form"/>
                        <p:commandButton value="导出" icon="fa fa-download"
                                         ajax="false">
                            <p:dataExporter type="xls" target="statisticRecordDataTable" fileName="消耗记录" postProcessor="#{businessIncomeListBean.postProcessXLS}"/>
                        </p:commandButton>
                    </f:facet>
                </p:toolbar>
            </div>
            <div class="ui-g ui-fluid" style="padding-top: 30px;">
                <div class="ui-g-12">
                    <div class="ui-g-12 card">
                        <h1>查询条件</h1>
                        <div class="ui-g-2">
                            <p:outputLabel value="渠道名称"/>
                        </div>
                        <div class="ui-g-4">
                            <p:selectCheckboxMenu value="#{businessIncomeListBean.$.channelId}"
                                                  filter="true" updateLabel="true" multiple="true"
                                                  filterMatchMode="contains">
                                <f:selectItems value="#{businessIncomeListBean.productList}"
                                               var="dic"
                                               itemLabel="#{dic.channelName}(#{dic.channelId})" itemValue="#{dic.id}"/>
                            </p:selectCheckboxMenu>
                        </div>
                        <div class="ui-g-2">
                            <p:outputLabel value="渠道分类"/>
                        </div>
                        <div class="ui-g-4">
                            <p:selectCheckboxMenu value="#{businessIncomeListBean.$.channelType}"
                                                  updateLabel="true" multiple="true" filter="true"
                                                  filterMatchMode="contains">
                                <f:selectItems
                                        value="#{businessIncomeListBean.dictionaries[DictionaryCode.CHANNEL_TYPE]}"
                                        var="dic"
                                        itemLabel="#{dic.name}" itemValue="#{dic.value1}"/>
                            </p:selectCheckboxMenu>
                        </div>
                        <div class="ui-g-2">
                            <p:outputLabel value="主体名称"/>
                        </div>
                        <div class="ui-g-4">
                            <p:selectOneMenu id="companyId" value="#{businessIncomeListBean.$.companyId}"
                                             filter="true" updateLabel="true"
                                             filterMatchMode="contains">
                                <f:selectItem itemLabel="- 请选择 -" itemValue=""
                                              noSelectionOption="true"/>
                                <f:selectItems value="#{businessIncomeListBean.companyList}"
                                               var="dic"
                                               itemLabel="#{dic.channelMain}" itemValue="#{dic.id}"/>
                            </p:selectOneMenu>
                        </div>

                        <div class="ui-g-2">
                            <p:outputLabel value="结算时间"/>
                        </div>
                        <div class="ui-g-4">
                            <p:calendar
                                    id="startTime" styleClass="autoWidthCalendar"
                                    value="#{businessIncomeListBean.$.startTime}"
                                    locale="#{constants:get('LOCALE')}"
                                    timeZone="#{constants:get('TIME_ZONE')}"
                                    pattern="#{constants:get('DATE_FORMAT')}"/>
                            <span>—</span>
                            <p:calendar
                                    id="endTime" styleClass="autoWidthCalendar"
                                    value="#{businessIncomeListBean.$.endTime}"
                                    locale="#{constants:get('LOCALE')}"
                                    timeZone="#{constants:get('TIME_ZONE')}"
                                    pattern="#{constants:get('DATE_FORMAT')}"/>
                        </div>

                        <div class="ui-g-2">
                            <p:outputLabel value="创建人"/>
                        </div>
                        <div class="ui-g-4">
                            <p:inputText id="creator" value="#{businessIncomeListBean.$.creator}"/>
                        </div>
                        <h:panelGroup id="allIncome">
                            <div class="ui-g-2">
                                <p:outputLabel value="结算总金额合计"/>
                            </div>
                            <div class="ui-g-4">
                                <p:outputLabel
                                        value="#{businessIncomeListBean.allIncome}"
                                        disabled="true"/>
                            </div>
                        </h:panelGroup>
                    </div>

                    <div class="ui-g-12 card">
                        <p:dataTable id="statisticRecordDataTable"
                                     style="width: 120%;text-align:center"
                                     var="record"
                                     value="#{businessIncomeListBean.lazyDataModel}"
                                     rows="#{constants:get('ROWS')}"
                                     lazy="true"
                                     paginator="true"
                                     paginatorTemplate="#{constants:get('PAGINATOR_TEMPLATE')}"
                                     rowsPerPageTemplate="#{constants:get('ROWS_PER_PAGE_TEMPLATE')}"
                                     emptyMessage="#{constants:get('EMPTY_MESSAGE')}"
                                     scrollable="true"
                                     scrollWidth="98%"
                                     reflow="true"
                                     resizableColumns="true">
                            <p:column headerText="操作" exportable="false" rendered="#{security:areAllGranted('BUSINESS_SECURITY')}">
                                <p:splitButton icon="fa fa-eye"
                                               process="@this"
                                               actionListener="#{businessIncomeListBean.onSee(record.id)}"
                                               oncomplete="PF('adding').show();" update="@form" rendered="#{security:areAllGranted('BUSINESS_SECURITY')}">
                                    <p:menuitem value="删除" icon="fa fa-remove"
                                                actionListener="#{businessIncomeListBean.onDelete(record.id)}"
                                                process="@this" update="@form" rendered="#{security:areAllGranted('BUSINESS_SECURITY')}">
                                        <p:confirm header="提示信息" message="是否确定删除?" icon="ui-icon-alert"/>
                                    </p:menuitem>
                                </p:splitButton>
                            </p:column>
                            <p:column headerText="结算时间" style="text-align:center;width: 70px">
                                <h:outputText value="#{record.settlementTime}">
                                    <f:convertDateTime pattern="#{constants:get('DATE_FORMAT')}"/>
                                </h:outputText>
                            </p:column>
                            <p:column headerText="渠道ID">
                                <h:outputText value="#{record.channelId}"/>
                            </p:column>
                            <p:column headerText="渠道名称">
                                <h:outputText value="#{record.channelName}"/>
                            </p:column>
                            <p:column headerText="渠道主体">
                                <h:outputText value="#{record.channelMain}"/>
                            </p:column>
                            <p:column headerText="所属产品">
                                <h:outputText value="#{businessProductInfoBean.translateProduct(record.belongAppId)}"/>
                            </p:column>
                            <p:column headerText="渠道分类" style="text-align:center;">
                                <h:outputText
                                        value="#{businessProductInfoBean.translate(DictionaryCode.CHANNEL_TYPE,record.channelType)}"/>
                            </p:column>
                            <p:column headerText="合作模式" style="text-align:center;">
                                <h:outputText
                                        value="#{businessProductInfoBean.translate(DictionaryCode.COOPERATION_MODE,record.copMode)}"/>
                            </p:column>
                            <p:column headerText="结算类别" style="text-align:center;">
                                <h:outputText
                                        value="#{businessProductInfoBean.translate(DictionaryCode.SETTLEMENT_CATEGORY,record.payMode)}"/>
                            </p:column>
                            <p:column headerText="结算方式" style="text-align:center;">
                                <h:outputText
                                        value="#{businessProductInfoBean.translate(DictionaryCode.SETTLEMENT_CYCLE,record.payType)}"/>
                            </p:column>

                            <p:column headerText="结算单价(元)" style="text-align:center;">
                                <h:outputText value="#{record.pricePay == null ?'-':record.pricePay}">
                                    <f:convertNumber pattern="###0.000"/>
                                </h:outputText>
                            </p:column>
                            <p:column headerText="结算数" style="text-align:center;">
                                <h:outputText value="#{record.count == null ?'-': record.count}"/>
                            </p:column>
                            <p:column headerText="结算金额" style="text-align:center;">
                                <h:outputText value="#{record.incomeAmount}">
                                    <f:convertNumber pattern="###0.00"/>
                                </h:outputText>
                            </p:column>
                            <p:column headerText="实际注册" style="text-align:center;">
                                <h:outputText value="#{record.realRegister == null ?'-': record.realRegister}"/>
                            </p:column>
                            <p:column headerText="实际成本" style="text-align:center;">
                                <h:outputText value="#{record.realRegister == null ?'-': record.incomeAmount/record.realRegister}">
                                    <f:convertNumber pattern="###0.00"/>
                                </h:outputText>
                            </p:column>
                            <p:column headerText="登录数" style="text-align:center;">
                                <h:outputText value="#{record.loginCount == null ?'-': record.loginCount}"/>
                            </p:column>
                            <p:column headerText="登陆成本" style="text-align:center;">
                                <h:outputText value="#{record.loginCount == null ?'-': record.incomeAmount/record.loginCount}">
                                    <f:convertNumber pattern="###0.00"/>
                                </h:outputText>
                            </p:column>
                            <p:column headerText="参与uv(截流前)" style="text-align:center;">
                                <h:outputText value="#{record.beforeRealCost == null ?'-': record.beforeRealCost}"/>
                            </p:column>
                            <p:column headerText="参与uv成本" style="text-align:center;">
                                <h:outputText value="#{record.beforeRealCost == null ?'-': record.incomeAmount/record.beforeRealCost}">
                                    <f:convertNumber pattern="###0.00"/>
                                </h:outputText>
                            </p:column>
                            <p:column headerText="创建人" style="text-align:center;">
                                <h:outputText value="#{record.creator}"/>
                            </p:column>
                            <p:column headerText="创建时间" style="text-align:center;width: 100px">
                                <h:outputText value="#{record.createTime}">
                                    <f:convertDateTime pattern="#{constants:get('DATETIME_FORMAT')}"/>
                                </h:outputText>
                            </p:column>
                        </p:dataTable>
                    </div>
                </div>
            </div>
            <p:dialog header="#{businessIncomeListBean.header}" widgetVar="adding" modal="true" closable="true"
                      resizable="true"
                      style="position: absolute;">
                <p:outputPanel id="add-dialog">
                    <div class="ui-g ui-fluid" style="width:1000px;">
                        <div class="ui-g-2">
                            <p:outputLabel value="渠道名称"/>
                        </div>
                        <div class="ui-g-4">
                            <p:selectOneMenu value="#{businessIncomeListBean.settlementIncome.channelId}"
                                             filter="true" updateLabel="true"
                                             filterMatchMode="contains" disabled="true">
                                <p:ajax event="change" listener="#{businessIncomeListBean.onSelectProduct()}"
                                        process="@this" update="add-dialog"/>
                                <f:selectItem itemLabel="- 请选择 -" itemValue=""
                                              noSelectionOption="true"/>
                                <f:selectItems value="#{businessIncomeListBean.productList}"
                                               var="dic"
                                               itemLabel="#{dic.channelName}(#{dic.channelId})" itemValue="#{dic.channelId}"/>
                            </p:selectOneMenu>
                        </div>
                        <div class="ui-g-2">
                            <p:outputLabel value="主体名称"/>
                        </div>
                        <div class="ui-g-4">
                            <p:selectOneMenu value="#{businessIncomeListBean.businessProduct.companyId}"
                                             filter="true" updateLabel="true"
                                             filterMatchMode="contains"
                            >
                                <f:selectItem itemLabel="- 请选择 -" itemValue=""
                                              noSelectionOption="true"/>
                                <f:selectItems value="#{businessIncomeListBean.companyList}"
                                               var="dic"
                                               itemLabel="#{dic.channelMain}" itemValue="#{dic.id}"/>
                            </p:selectOneMenu>
                        </div>
                        <div class="ui-g-2">
                            <p:outputLabel value="结算方式"/>
                        </div>
                        <div class="ui-g-4">
                            <p:selectOneMenu value="#{businessIncomeListBean.settlementIncome.payMode}" disabled="true">
                                <f:selectItem itemLabel="- 请选择 -" itemValue=""
                                              noSelectionOption="true"/>
                                <f:selectItems
                                        value="#{businessIncomeListBean.dictionaries[DictionaryCode.SETTLEMENT_CATEGORY]}"
                                        var="dic"
                                        itemLabel="#{dic.name}" itemValue="#{dic.value1}"/>
                            </p:selectOneMenu>
                        </div>

                        <div class="ui-g-2">
                            <p:outputLabel value="结算模式"/>
                        </div>
                        <div class="ui-g-4">
                            <p:selectOneMenu value="#{businessIncomeListBean.settlementIncome.payType}" disabled="true"
                            >
                                <f:selectItem itemLabel="- 请选择 -" itemValue=""
                                              noSelectionOption="true"/>
                                <f:selectItems
                                        value="#{businessIncomeListBean.dictionaries[DictionaryCode.SETTLEMENT_CYCLE]}"
                                        var="dic"
                                        itemLabel="#{dic.name}" itemValue="#{dic.value1}"/>
                            </p:selectOneMenu>
                        </div>
                        <h:panelGroup rendered="#{businessIncomeListBean.settlementIncome.payMode==3
                                    || businessIncomeListBean.settlementIncome.payMode==4
                                    || businessIncomeListBean.settlementIncome.payMode==1
                                    || businessIncomeListBean.settlementIncome.payMode==2}">
                            <div class="ui-g-4">
                                <p:outputLabel value="结算数"/>
                            </div>
                            <div class="ui-g-8">
                                <p:inputNumber value="#{businessIncomeListBean.settlementIncome.count}"
                                >
                                    <p:ajax listener="#{businessIncomeListBean.onChangeIncomeAmount()}" event="keyup"
                                            update="incomeAmount"/>
                                </p:inputNumber>
                            </div>
                        </h:panelGroup>

                        <div class="ui-g-2">
                            <p:outputLabel value="结算金额（总消耗）"/>
                        </div>
                        <div class="ui-g-4">
                            <p:inputNumber id="incomeAmount"
                                           value="#{businessIncomeListBean.settlementIncome.incomeAmount}"
                                           disabled="#{businessIncomeListBean.settlementIncome.payMode==1
                                    || businessIncomeListBean.settlementIncome.payMode==2
                                    || businessIncomeListBean.settlementIncome.payMode==3
                                    || businessIncomeListBean.settlementIncome.payMode==4}">
                            </p:inputNumber>
                        </div>


                        <div class="ui-g-2">
                            <p:outputLabel value="结算日期"/>
                        </div>
                        <div class="ui-g-4">
                            <p:calendar
                                    value="#{businessIncomeListBean.settlementIncome.settlementTime}"
                                    locale="#{constants:get('LOCALE')}"
                                    timeZone="#{constants:get('TIME_ZONE')}"
                                    pattern="#{constants:get('DATE_FORMAT')}">
                            </p:calendar>
                        </div>
                        <h:panelGroup rendered="#{businessIncomeListBean.settlementIncome.payMode==1
                                    || businessIncomeListBean.settlementIncome.payMode==2}">
                            <div class="ui-g-2">
                                <p:outputLabel value="结算日期尾"/>
                            </div>
                            <div class="ui-g-4">
                                <p:calendar
                                        value="#{businessIncomeListBean.endTime}"
                                        locale="#{constants:get('LOCALE')}"
                                        timeZone="#{constants:get('TIME_ZONE')}"
                                        pattern="#{constants:get('DATE_FORMAT')}">
                                </p:calendar>
                            </div>
                        </h:panelGroup>


                        <div class="ui-g-12" style="padding: 0">
                            <div class="ui-g-2">
                                <p:outputLabel value="备注"/>
                            </div>
                            <div class="ui-g-10">
                                <p:inputText value="#{businessIncomeListBean.settlementIncome.remark}"/>
                            </div>
                        </div>

                        <div class="ui-g-12">
                            <p:commandButton value="保存" style="float: right"
                                             actionListener="#{businessIncomeListBean.onSave()}"
                                             process="@this add-dialog" rendered="#{security:areAllGranted('BUSINESS_SECURITY')}"/>
                            <p:commandButton value="自动计算" style="float: right"
                                             actionListener="#{businessIncomeListBean.autoCount()}"
                                             rendered="#{(businessIncomeListBean.settlementIncome.payMode==1
                                    || businessIncomeListBean.settlementIncome.payMode==2) and security:areAllGranted('BUSINESS_ATUO')}"
                                             process="@this add-dialog"/>
                        </div>
                    </div>
                </p:outputPanel>
            </p:dialog>

        </h:form>
    </ui:define>
</ui:composition>